package com.andy.qhb.websocket;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.andy.qhb.game.GameUser;
import com.andy.qhb.utils.GoConstant;

public class WebSession implements MySocketSession{

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	private HttpSession httpSession;
	
	private Object respMsg;
	
	public WebSession() {
	}
	
	public WebSession(HttpSession httpSession) {
		this.httpSession = httpSession;
	}

	@Override
	public void setAttr(String name, Object value) {
		if(httpSession != null){
			httpSession.setAttribute(name, value);
		}
	}

	@Override
	public Object getAttr(String name) {
		if(httpSession != null){
			return httpSession.getAttribute(name);
		}
		return null;
	}

	@Override
	public Object removeAttr(String name) {
		httpSession.removeAttribute(name);
		return null;
	}

	@Override
	public GameUser getUser() {
		return (GameUser) getAttr(GoConstant.USER_SESSION_KEY);
	}

	@Override
	public void sendMessage(String message) {
		if(httpSession == null)return;
		if(logger.isInfoEnabled()){
			logger.info("发送消息:{}", message);
		}
	}
	
	public Object sendMsg(){
		return respMsg;
	}
	public void setRespMsg(Object respMsg){
		this.respMsg = respMsg;
	}

	@Override
	public void invalidate() {
		httpSession.invalidate();
		httpSession = null;
	}

}
